﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace DeskNotes
{
    public partial class CaptionBar : UserControl
    {
        // Events raised by CaptionBar when mouse interaction happens.
        // Must be registered by receiver NoteForm
        public event EventHandler<MouseEventArgs> CaptionBarMouseDownEvent;
        public event EventHandler<MouseEventArgs> CaptionBarMouseUpEvent;
        public event EventHandler<MouseEventArgs> CaptionBarMouseMoveEvent;
        public event EventHandler CaptionBarMouseLeaveEvent;
                
        enum Modes {Show, Hide};
        Modes mode;
        NoteForm noteFormParent;
        public static int maxHeight = 24;
        private bool cancelOperation;
        static Image texture = DeskNotes.Properties.Resources.CaptionBar_brush;

        static CaptionBar()
        {
            maxHeight = WinImports.GetSystemMetrics(WinImports.SM_CYCAPTION);
            Logger.WriteLine("CaptionBar maxHeight set to: {0} pixels.", maxHeight);
        }

        public CaptionBar()
        {
            InitializeComponent();
            this.Height = 1;
        }

        public CaptionBar(NoteForm parent)
            : this()
        {
            noteFormParent = parent;
        }
        private void CaptionBar_MouseDown(object sender, MouseEventArgs e)
        {
            //noteFormParent.cBar_MouseDown(sender, e);
            if (CaptionBarMouseDownEvent != null)
            {
                CaptionBarMouseDownEvent(sender, e);
            }
        }

        private void CaptionBar_MouseUp(object sender, MouseEventArgs e)
        {
            //noteFormParent.cBar_MouseUp(sender, e);
            if (CaptionBarMouseUpEvent != null)
                CaptionBarMouseUpEvent(sender, e);
        }

        private void CaptionBar_MouseMove(object sender, MouseEventArgs e)
        {
            //noteFormParent.cBar_MouseMove(sender, e);
            if (CaptionBarMouseMoveEvent != null)
                CaptionBarMouseMoveEvent(sender, e);
        }

        private void CaptionBar_MouseLeave(object sender, EventArgs e)
        {
            //noteFormParent.NoteForm_MouseLeave(sender, e);
            if (CaptionBarMouseLeaveEvent != null)
                CaptionBarMouseLeaveEvent(sender, e);
        }

        /// <summary>
        /// Hides the control 
        /// </summary>
        public new void Hide()
        {
            cancelOperation = false;
            mode = Modes.Hide;
            timerShowHide.Enabled = true;
        }
        /// <summary>
        /// Shows the control with timer in several steps
        /// </summary>
        public new void Show()
        {
            this.Height = 1;
            this.Visible = true;
            this.BringToFront();
            cancelOperation = false;
            mode = Modes.Show;
            timerShowHide.Enabled = true;
        }
        /// <summary>
        /// Shows timer pixel by pixel from top to down, rolling like effect 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerShowHide_Tick(object sender, EventArgs e)
        {
            if (cancelOperation)
            {
                timerShowHide.Enabled = false;
                this.Hide();
                return;
            }
            switch (mode)
            {
                case Modes.Show:
                    if (this.Height < CaptionBar.maxHeight)
                        this.Height++;
                    else
                        timerShowHide.Enabled = false;
                    break;
                case Modes.Hide:
                    if (this.Height > 0)
                        this.Height--;
                    else
                    {
                        timerShowHide.Enabled = false;
                        this.Visible = false;
                        this.SendToBack();
                    }
                    break;
            }
        }

        public bool CancelOperation
        {
            get { return cancelOperation; }
            set { 
                cancelOperation = value;
                if (cancelOperation)
                    timerShowHide.Enabled = false;
            }
        }

        public int MaxHeight {
            get { return maxHeight; }
            set { maxHeight = value; }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //Logger.WriteLine("pictureBox1_Paint called");
            base.OnPaint(e);
            Graphics g = e.Graphics;
            System.Drawing.TextureBrush myBrush = new System.Drawing.TextureBrush(texture);
            g.FillRectangle(myBrush, 0, 0, this.Width, this.Height);
            myBrush.Dispose();
            //g.Dispose(); // should not be disposed, otherwise app crashes at getHDC call.
        }

        
    }
}